using System;
using System.Collections.Generic;

namespace Utils
{
    public static class LogHelper
    {
        private enum LogLevel
        {
            Trace, // 最低级别
            Debug, // 调试信息
            Info, // 常规信息
            Warning, // 警告
            Error, // 错误
            // Fatal, // 严重错误
        }

        //===================
        // 配置选项
        //===================
        public static bool ShowTime = false; // 显示时间戳
        public static bool ShowCategory = true; // 显示分类标签
        public static bool IsEnabled = true; // 全局开关

        // 日志颜色配置（仅编辑器生效）
        private static readonly Dictionary<LogLevel, string> _logColors = new Dictionary<LogLevel, string>()
        {
            { LogLevel.Trace, "#AAAAAA" }, // 灰色
            { LogLevel.Debug, "#00AAFF" }, // 蓝色
            { LogLevel.Info, "#FFFFFF" }, // 白色
            { LogLevel.Warning, "#FFAA00" }, // 黄色
            { LogLevel.Error, "#FF2200" }, // 红色
            // { LogLevel.Fatal, "#FF00FF" } // 紫色
        };

        //===================
        // 核心日志方法
        //===================
        private static void LogInternal(LogLevel level, string message, string category, UnityEngine.Object context)
        {
            if (!IsEnabled)
                return;

            // 打包后禁用 Trace/Debug 日志
#if !UNITY_EDITOR
            if (level == LogLevel.Trace || level == LogLevel.Debug) 
                return;
#endif

            // 构建日志前缀
            string prefix = BuildLogPrefix(level, category);

            // 打包后移除颜色标签
#if UNITY_EDITOR
            string formatted = $"<color={_logColors[level]}>{prefix}{message}</color>";
#else
            string formatted = $"{prefix}{message}";
#endif

            // 根据级别输出
            DispatchLog(level, formatted, context);
        }

        // 构建日志前缀（时间/级别/分类）
        private static string BuildLogPrefix(LogLevel level, string category)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            if (ShowTime)
                sb.Append($"[{DateTime.Now:HH:mm:ss}] ");

            sb.Append($"[{level}] ");

            if (ShowCategory && !string.IsNullOrEmpty(category))
                sb.Append($"[{category}] ");

            return sb.ToString();
        }

        // 分发到 Unity 的日志系统
        private static void DispatchLog(LogLevel level, string message, UnityEngine.Object context)
        {
            switch (level)
            {
                case LogLevel.Warning:
                    UnityEngine.Debug.LogWarning(message, context);
                    break;
                case LogLevel.Error:
                    // case LogLevel.Fatal:
                    UnityEngine.Debug.LogError(message, context);
                    break;
                default:
                    UnityEngine.Debug.Log(message, context);
                    break;
            }
        }

        //===================
        // 公开日志方法组
        //===================

        // Trace 系列
        public static void Trace(string message, string category = "Default")
            => LogInternal(LogLevel.Trace, message, category, null);

        public static void Trace(string message, UnityEngine.Object context)
            => LogInternal(LogLevel.Trace, message, "Default", context);

        public static void Trace(string message, string category, UnityEngine.Object context)
            => LogInternal(LogLevel.Trace, message, category, context);

        // Debug 系列
        public static void Debug(string message, string category = "Default")
            => LogInternal(LogLevel.Debug, message, category, null);

        public static void Debug(string message, UnityEngine.Object context)
            => LogInternal(LogLevel.Debug, message, "Default", context);

        public static void Debug(string message, string category, UnityEngine.Object context)
            => LogInternal(LogLevel.Debug, message, category, context);

        // Info 系列
        public static void Info(string message, string category = "Default")
            => LogInternal(LogLevel.Info, message, category, null);

        public static void Info(string message, UnityEngine.Object context)
            => LogInternal(LogLevel.Info, message, "Default", context);

        public static void Info(string message, string category, UnityEngine.Object context)
            => LogInternal(LogLevel.Info, message, category, context);

        // Warning 系列
        public static void Warning(string message, string category = "Default")
            => LogInternal(LogLevel.Warning, message, category, null);

        public static void Warning(string message, UnityEngine.Object context)
            => LogInternal(LogLevel.Warning, message, "Default", context);

        public static void Warning(string message, string category, UnityEngine.Object context)
            => LogInternal(LogLevel.Warning, message, category, context);

        // Error 系列
        public static void Error(string message, string category = "Default")
            => LogInternal(LogLevel.Error, message, category, null);

        public static void Error(string message, UnityEngine.Object context)
            => LogInternal(LogLevel.Error, message, "Default", context);

        public static void Error(string message, string category, UnityEngine.Object context)
            => LogInternal(LogLevel.Error, message, category, context);

        // Fatal 系列
        /*public static void Fatal(string message, string category = "Default")
            => LogInternal(LogLevel.Fatal, message, category, null);

        public static void Fatal(string message, UnityEngine.Object context)
            => LogInternal(LogLevel.Fatal, message, "Default", context);

        public static void Fatal(string message, string category, UnityEngine.Object context)
            => LogInternal(LogLevel.Fatal, message, category, context);*/
    }
}